# -*- coding:utf-8 -*-

from django.http import HttpResponse
from django.template.context import Context
from django.template.loader import get_template
from emp import models
from emp.manager import saveEmp, saveDep, saveCate,showAll


def getCate(req, vals):
    cate = vals.split('=')
    saveCate(cate[0], cate[1])
    return HttpResponse("ok")

def getEmp(req,vals):
    emp = vals.split('=')
    #print  emp[0],'=',len(emp[1].split(',')),vals
    saveEmp(emp[0],emp[1].split(','))
    return HttpResponse("ok")

def getDep(req, vals):
    print vals
    dep = vals.split('=')
    saveDep(dep[0], dep[1].split(','))
    return HttpResponse("ok")


from django.shortcuts import render_to_response

def getEmpById(req,id):
    return HttpResponse(models.Employee.objects.get(pk=int(id)))

def listCate(req):
    return render_to_response('emp/build.html',{'cates': showAll()})

def deleteEmp(req,id):
    return None

def deleteEmp(req, id):
    return HttpResponse(models.Employee.objects.get(pk=int(id)).__str__() + " 删除成功!")

def download(req):
    html = get_template('emp/build.html').render(Context({'cates': showAll(), 'isDownload': True}))
    #print html.encode('utf-8')
    #open(u'./index.html','w').write(html.encode('utf-8'))
    response = HttpResponse(html)
    response['Content-Disposition'] = 'attachment; filename=index.html'
    return response

    